//
//  MainTabBar.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/6/23.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {


    //定义按钮个数
    
    private let buttonCount = 5
    
    
    //按钮重新布局
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = bounds.width/CGFloat(buttonCount)
        
        let rect = CGRectMake(0, 0, w, bounds.height)
        
        
        var index = 0
    //遍历所有子控件
        for v in subviews{
        
            //判断控件,排除UIButton
            if v is UIControl && !(v is UIButton){
            
                v.frame = CGRectOffset(rect, CGFloat(index) * w, 0)
                
                index += index == 1 ? 2 : 1
                
            }
        }
        
        //加号的frame
        compoesButton.frame = CGRectOffset(rect, 2 * w, 0)
        
        
    }
    
    
    
    lazy var compoesButton:UIButton = {
    
        //实例化按钮
        let btn = UIButton()
        
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
        //添加到tabBar
        self.addSubview(btn)
        
        return btn
    
    }()
    
    
}
